home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 01 - Three and One / Solution.cp < prev    next >
Encoding:
Text File  |  1998-06-19  |  2.9 KB  |  124 lines  |  [TEXT/CWIE]

  1. /*
  2. Problem 01 - Mode Sort
  3.  
  4. This problem is to sort an input string of N characters, N<1000000, based on
  5. the number of times a character occurs in the input.  The most frequently
  6. occurring character should be sorted to the front of the string, followed by
  7. the next most frequently occurring character, etc.  For characters occurring
  8. the same number of times, the character that occurs first in the input should
  9. be sorted to the front.
  10.  
  11. Header specification
  12.  
  13. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile );
  14.  
  15. Input specification
  16.  
  17. The infile input file contains the characters.  Input characters other than
  18. those printable low ascii characters c, 0x20<c<0x7f, must be ignored.
  19.  
  20. Output specification
  21.  
  22. The outfile must be created and then filled with the sorted characters.  It's
  23. final length should be exactly the same as the count of characters in the
  24. allowed range (0x20<c<0x7f) (which may be shorter than the infile file length).
  25.  
  26. Sample input
  27.  
  28. abcdefghabbcccdddeee
  29. or
  30. 012345678911234567892123456789312345678941234567895123456789612345678971234567891
  31.  
  32. Sample output
  33.  
  34. ccccddddeeeebbbaafgh
  35. or
  36. 111111111122222222233333333344444444455555555566666666677777777788888888999999990
  37. */
  38.  
  39. #include "Solution.h"
  40. #include <algorithm>
  41.  
  42. // Fill in your solution and then submit this folder
  43.  
  44. // Team Name: Three and one!
  45.  
  46. struct CharacterWithFrequency {
  47.     unsigned char character;
  48.     unsigned frequency;
  49.     bool operator <(const CharacterWithFrequency& other) const;
  50. };
  51. __MSL_FIX_ITERATORS__(CharacterWithFrequency);
  52.  
  53. static CharacterWithFrequency table[256];
  54.  
  55. bool
  56. CharacterWithFrequency::operator <(const CharacterWithFrequency& other) const
  57. {
  58.     return frequency > other.frequency;
  59. }
  60.  
  61. static void
  62. SetUpCharacters()
  63. {
  64.     for (unsigned index = 0; index < 256; index++) {
  65.         table[index].character = index;
  66.         table[index].frequency = 0;
  67.     }
  68. }
  69.  
  70. static OSErr
  71. ReadCharacters(short inRefNum)
  72. {
  73.     while (1) {
  74.         long count = 1;
  75.         unsigned char character;
  76.         OSErr error = FSRead(inRefNum, &count, &character);
  77.         if (error == eofErr)
  78.             return noErr;
  79.         if (error != noErr)
  80.             return error;
  81.         if (character <= 0x20 || character >= 0x7F)
  82.             continue;
  83.         table[character].frequency += 1;
  84.     }
  85. }
  86.  
  87. static OSErr
  88. WriteCharacters(short outRefNum)
  89. {
  90.     std::sort(&table[0], &table[256]);
  91.     
  92.     for (unsigned index = 0; index < 256; index++) {
  93.         while (table[index].frequency != 0) {
  94.             long count = 1;
  95.             OSErr error = FSWrite(outRefNum, &count, &table[index].character);
  96.             if (error != noErr)
  97.                 return error;
  98.             table[index].frequency -= 1;
  99.         }
  100.     }
  101.     return noErr;
  102. }
  103.  
  104. pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile)
  105. {
  106.     OSErr error;
  107.     short inRefNum;
  108.     error = FSpOpenDF(infile, fsRdPerm, &inRefNum);
  109.     if (error != noErr)
  110.         return error;
  111.     
  112.     short outRefNum;
  113.     error = FSpCreate(outfile, 'CWIE', 'TEXT', 0);
  114.     error = FSpOpenDF(outfile, fsCurPerm, &outRefNum);
  115.     if (error != noErr)
  116.         return error;
  117.  
  118.     SetUpCharacters();
  119.     error = ReadCharacters(inRefNum);
  120.     if (error != noErr)
  121.         return error;
  122.     return WriteCharacters(outRefNum);
  123. }
  124.